﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Soap;
using System.Text;
using CDSBDemandControl.Model;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using System.Threading;
using CDSBDemandControl.Configuration;
using System.Xml.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace CDSBDemandControl.Communication
{
    public class XMLDemandAccess:IDisposable
    {
        public static List<DemandModel> GetDemandCollectionFromXML(string path)
        {
            List<DemandModel> DemandCollection = new List<DemandModel>();

            FileStream FSControlService;

            XMLDemandAccess.AguardaSemaforo();

            FSControlService = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);

            BinaryFormatter BFDemand = new BinaryFormatter();

            DemandCollection = Translator.DemandModelTranslator.Translate((DemandCollection)BFDemand.Deserialize(FSControlService));

            //Stream fechado
            FSControlService.Close();
            FSControlService.Dispose();
            BFDemand = null;

            return DemandCollection;
        }

        public static List<ScenarioModel> GetScenariosAndTasksFromXML(string path)
        {
            List<ScenarioModel> ScenariosAndTasks = new List<ScenarioModel>();

            FileStream FSControlService;

            XMLDemandAccess.AguardaSemaforo();

            FSControlService = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);

            BinaryFormatter BFDemand = new BinaryFormatter();

            ScenariosAndTasks = ((List<ScenarioModel>)BFDemand.Deserialize(FSControlService));

            //Stream fechado
            FSControlService.Close();
            FSControlService.Dispose();
            BFDemand = null;

            return ScenariosAndTasks;
        }

        /// <summary>
        /// Bug List
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static List<DemandModel> GetBugCollectionFromXML(string path)
        {
            List<DemandModel> DemandCollection = new List<DemandModel>();

            FileStream FSControlService;

            XMLDemandAccess.AguardaSemaforo();

            FSControlService = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);

            BinaryFormatter BFDemand = new BinaryFormatter();

            DemandCollection = Translator.DemandModelTranslator.Translate((DemandCollection)BFDemand.Deserialize(FSControlService));

            //Stream fechado
            FSControlService.Close();
            FSControlService.Dispose();
            BFDemand = null;

            return DemandCollection;
        }

        /// <summary>
        /// 
        /// </summary>
        private static void AguardaSemaforo()
        {
            FileStream FSControlService;

            bool waiting = true;
            int maxMsToWait = 2500;
            while (waiting && maxMsToWait > 0)
            {
                FSControlService = File.OpenRead(ConfigManager.GetLockFilePath());
                int status = FSControlService.ReadByte();
                FSControlService.Close();
                FSControlService.Dispose();

                if (status == 65)
                {
                    waiting = false;
                }
                else
                {
                    Thread.Sleep(100);
                    maxMsToWait -= 100;
                }
            }

            if (maxMsToWait <= 0)
                throw new Exception("Time out lendo arquivo Control.sem, aguarde alguns segundos e tente novamente");

        }
        
        public static DateTime GetFileDate(string path) 
        {
            return File.GetLastWriteTime(path);
        }

        #region IDisposable Members

        public void Dispose()
        {
            
        }

        #endregion
    }
}
